<?php
namespace App\Http\Controllers\Home;

use App\Models\agentOrder;
use App\Models\Goods;
use App\Models\Order;
use App\Models\Securitycodeout;

Class OrderController extends BaseController
{

    public function __construct(Order $order, Securitycodeout $securitycodeout, Goods $goods, agentOrder $agentOrder)
    {
        parent::__construct();
        $this->order = $order;
        $this->securitycodeout = $securitycodeout;
        $this->goods = $goods;
        $this->agentOrder = $agentOrder;
    }

    public function getIndex()
    {
        return view('home.index');
    }

    public function getCheckCode()
    {
        return view('home.check_code');
    }

    public function postCheckCode()
    {
        $res = $this->securitycodeout->checkCode(request());
        return redirect()->back()->with('message', $res)->withInput();
    }

    public function getOrderCount()
    {
        if (request('start') || request('end')) {
            $num = $this->order->orderTotal(request());
        }
        return view('home.order_count', compact('num'));
    }


    public function getFxOrder()
    {
        $rows = $this->order->getFxData();
        return view('home.order_fenxiao', compact('rows'));
    }

    public function getOrderList()
    {
        $rows = $this->order->getMyOrderList(request());
        return view('home.order', compact('rows'));
    }

    public function getOrderDetail()
    {
        $data['order'] = $this->order->getOrderInfoById(request('id'));
        $data['goods'] = $this->goods->getGoodsInfoById($data['order']['goods_id']);
        return view('home.order_detail', compact('data'));
    }

    public function postAddAdres()
    {
        $gid = intval(request('gid'));
        $num = intval(request('num'));
        $res = $this->goods->checkGoodsNum($gid, $num);
        if (!$res['status']) {
            return redirect()->back()->with('message', $res['msg']);
        }
        return view('home.order_adres', compact('gid', 'num', 'res'));
    }

    public function postAddOrder()
    {
        $res = $this->order->checkOrderData(request());
        if (!$res['status']) {
            return redirect()->back()->with('message', $res['msg'])->withInput();
        }
        $res = $this->order->addOrder(request());
        if (!$res['status']) {
            return redirect()->back()->with('message', $res['msg'])->withInput();
        }
        return redirect(urlHome('order', 'getOrderList'));
    }

    public function postOrderDel()
    {
        $res = $this->order->deleteOrder(request());
        return ajax_return($res);
    }

    public function getScan()
    {
        return view('home.scan');
    }

    public function postSendGoods()
    {
        $res = $this->order->sendNextLevOrder(request());
        return ajax_return($res);
    }

    public function getCheckOrderSn()
    {
        $res = $this->order->checkOrderSn(request('order_sn', ''));
        return ajax_return($res);
    }

    public function postChangeOrderStatus()
    {
        $res = $this->order->sendGoods(request());
        return ajax_return($res);
    }
}